'use strict';

const Controller = require('egg').Controller;
const moment = require('moment')
const md5 = require('md5')
class Admin extends Controller {
  // 管理员列表
  async adminList() {
    const {
      ctx
    } = this;
    const Op = ctx.model.Sequelize.Op;
    let page = ctx.request.body.page
    let pageshow = ctx.request.body.pageshow
    let user = await ctx.model.User.findAndCountAll({
      where: {
        user_id: {
          // 查找id大于1
          [Op.gt]: 1
        }
      },
      limit: pageshow,
      offset: (page - 1) * pageshow
    })
    return ctx.body = {
      code: 200,
      data: {
        list: user.rows.map(item => {
          return {
            user_id: item.user_id,
            username: item.username,
            lock: item.lock,
            auth: item.auth,
            updated_at: moment(item.updated_at * 1000).format('YYYY-MM-DD HH:mm:ss')
          }
        }),
        count: user.count
      },
      msg: '操作成功'
    }
  }
  async adminOperation() {
    const {
      ctx
    } = this;
    let type = ctx.request.body.type
    let role = ctx.request.body.role
    let user_id = ctx.request.body.user_id
    let status = ctx.request.body.status
    let username = ctx.request.body.username
    let password = ctx.request.body.password
  
    if (type == 'add') {
      // 新增管理员
      await ctx.model.User.findOrCreate({
          where: {
            username: username
          },
          defaults: {
            lock: '1',
            auth: role,
            password: md5(password),
            created_at: Date.parse(new Date) / 1000,
            updated_at: Date.parse(new Date) / 1000
          }
        })
        .then(([user, created]) => {
          if (created) {
            return ctx.body = {
              code: 200,
              data: {},
              msg: '操作成功'
            }
          } else {
            return ctx.body = {
              code: 0,
              data: {},
              msg: '账号已存在'
            }
          }
        })
    } else if (type == 'save') {
      // 编辑管理员
      await ctx.model.User.update({
        username,
        password: md5(password),
        auth: role,
        updated_at: Date.parse(new Date) / 1000
      }, {
        where: {
          user_id
        }
      })
      return ctx.body = {
        code: 200,
        data: {},
        msg: '操作成功'
      }
    } else if (type == 'status') {
      if (status == 0) {
        // 封禁
        await ctx.model.User.update({
          lock: '0',
          updated_at: Date.parse(new Date) / 1000
        }, {
          where: {
            user_id
          }
        })
        return ctx.body = {
          code: 200,
          data: {},
          msg: '操作成功'
        }
      } else if (status == 1) {
        // 开启
        await ctx.model.User.update({
          lock: '1',
          updated_at: Date.parse(new Date) / 1000
        }, {
          where: {
            user_id
          }
        })
        return ctx.body = {
          code: 200,
          data: {},
          msg: '操作成功'
        }
      }
    } else if (type == 'del') {
      // 删除管理员
      await ctx.model.User.destroy({
        where: {
          user_id
        }
      })
      return ctx.body = {
        code: 200,
        data: {},
        msg: '操作成功'
      }
    }
  }
}

module.exports = Admin;